package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.*;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.IBusinessRecordService;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 业务记录 前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-17
 */
@Api(tags ="客户交往记录")
@CrossOrigin
@RestController
@RequestMapping("/business-record")
public class BusinessRecordController {


    @Autowired
    IBusinessRecordService businessRecordService;

    @Autowired
    IAccountManagerService accountManagerService;

    @Autowired
    ICustomerService customerService;

    @Autowired
    CookieUtil cookieUtil;

    /**
     * 该客户的全部交往记录
     * */
    @ApiOperation(value = "查询该客户的全部交往记录")
    @ApiImplicitParam(name = "id",value = "客户的id")
    @RequestMapping(value = "list/{id}", method = RequestMethod.POST)
    public ResultUtil listAll(@PathVariable("id") Integer id) {

        Customer customer = customerService.getById(id);

        //根据客户编号查询交往记录
        QueryWrapper<BusinessRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("br_customer_id", customer.getCustomerId());
        List<BusinessRecord> list = businessRecordService.list(wrapper);

        //根据客户编号查询客户经理
        QueryWrapper<AccountManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("am_id",customer.getCustomerCid());
        AccountManager one = accountManagerService.getOne(queryWrapper);

        for (BusinessRecord businessRecord : list) {
            businessRecord.setCustomer(customer);
            businessRecord.setAccountManager(one);
        }


        return ResultUtil.resObj(list);

    }

    /**
     * 添加记录
     * */
    @Auth("AM")
    @ApiOperation(value = "添加一条交往记录")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ResultUtil add(BusinessRecord businessRecord, HttpServletRequest request){

        AccountManager accountManager = (AccountManager) cookieUtil.getObject(request);


        businessRecord.setBrAccountManagerId(accountManager.getId());

        if (businessRecordService.save(businessRecord)) {
            return ResultUtil.resMsgOK("添加成功");
        }
        return ResultUtil.resMsgError("添加失败");
    }


    /**
     * 修改记录
     * */
    @ApiOperation(value = "修改一条记录")
    @RequestMapping(value = "update",method = RequestMethod.PUT)
    public ResultUtil update(BusinessRecord businessRecord){

        if (businessRecordService.updateById(businessRecord)) {
            return ResultUtil.resMsgOK("更新成功");
        }
        return ResultUtil.resMsgError("更新失败");
    }


    /**
     * 删除记录
     * */
    @ApiOperation(value = "删除一条记录")
    @ApiImplicitParam(name = "id",value = "记录的id")
    @RequestMapping(value = "del/{id}",method = RequestMethod.DELETE)
    public ResultUtil del(@PathVariable("id") Integer id){

        if (businessRecordService.removeById(id)) {
            return ResultUtil.resMsgOK("删除成功");
        }
        return ResultUtil.resMsgError("删除失败");
    }
}
